//导入path和fs模块
const fs = require('fs');
const path = require('path');

//匹配正则
let regStyle = /<style>[\s\S]*<\/style>/;
let regScript = /<script>[\s\S]*<\/script>/;

//读取文件
let file = fs.readFile(path.join(__dirname,'../javascript/052.轮播图.html'),'utf8',function(err,data){
    if(err){
        return console.log(err.message);
    }
    removeCss(data);
    removeScript(data);
    removeHtml(data);
    
})


//定义一个移除css的函数
function removeCss(htmlstr){
    let r1 = regStyle.exec(htmlstr);
    let css = r1[0].replace('<style>',' ').replace('</style>',' ');
    // console.log(css);
    //将css封装到练习.css文件夹下
    fs.writeFile(path.join(__dirname,'./练习/轮播图.css'),css,err=>{
        if(err){
         return console.log(err.message);
        }
        console.log("css写入成功！！");
    })
}

//定义一个移除script的函数
function removeScript(htmlstr){
    let r2 = regScript.exec(htmlstr);
    let script = r2[0].replace('<script>',' ').replace('</script>',' ');
    // console.log(script);
    fs.writeFile(path.join(__dirname,'./练习/轮播图.js'),script,err=>{
        if(err){
            return console.log(err.message);
        }else{
            console.log('script写入成功！！');
        }
    })
}

function removeHtml(htmlstr){
    let html = htmlstr.replace(regStyle,'<link rel="stylesheet" href="./轮播图.css">')
    .replace(regScript,'<script src="./轮播图.js"></script>');
    console.log(html);
    fs.writeFile(path.join(__dirname,'./练习/练习.html'),html,err=>{
        if(err){
            return console.log(err.message);
        }else{
            console.log('html写入成功！！');
        }
    })
}
